package cn.lctvu.servlet.admin;

import cn.lctvu.bean.User;
import cn.lctvu.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/admin/LoginServlet")
public class LoginServlet extends HttpServlet {

    private UserService userService=new UserService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        request.setCharacterEncoding("utf-8");
    String username=request.getParameter("username");

    String password=request.getParameter("password");
    System.out.println("username:"+username+"password:"+password);
    User user=userService.getUserByNameAndPass(username,password);
    if(user==null){
        out.print("<script language='javascript'>alert('用户名或密码有误,请重新登录');document.location.href='login.jsp';</script>");
        return;
    }
        HttpSession session=request.getSession();
    session.setAttribute("user",user);
    response.sendRedirect("index.jsp");


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request,response);
    }
}
